<?php
/**
 * xhr communication class (requ. yui js for comm.)
 *
 * PHP version 5
 *
 * @category  PHP
 * @package   PHPMY
 * @author    Sven Ruttloff, www.sruttloff.de <service@sruttloff.de>
 * @copyright 2010 Sven Ruttloff, www.sruttloff.de
 * @license   http://www.sruttloff.de/cms/files/licence_en_lgpl.txt LGPL
 * @version   SVN: $Id: xhranswer.class.php 118 2010-08-19 18:00:12Z sruttloff@googlemail.com $
 * @link      phpmy
 */
require_once dirname(__FILE__) . '/services_json.class.php';
/**
 * xhr communication class (requ. yui js for comm.)
 *
 * @category  PHP
 * @package   PHPMY
 * @author    Sven Ruttloff, www.sruttloff.de <service@sruttloff.de>
 * @copyright 2010 Sven Ruttloff, www.sruttloff.de
 * @license   http://www.sruttloff.de/cms/files/licence_en_lgpl.txt LGPL
 * @link      phpmy
 */
class xhranswer
{
    /**
     * @var object instance
     */
    protected static $INSTANCE;
    /**
     * @var object answer
     */
    protected $XHRANSWER;
    /**
     * datas to bypas json rendering, while already parsed
     * @var array
     */
    protected $XHRANSWERBYPASS = array();
    /**
     * instantiate essential objects and make them avail. as subobjects
     *
     * @return	void
     */
    private function __construct()
    {
        // init result
        $this->XHRANSWER = new stdclass();
        $this->XHRANSWER->Results = new stdclass();
        // input field values to update
        $this->XHRANSWER->Results->UpdateValue = array();
        // replace content
        $this->XHRANSWER->Results->replaceContentById = array();
        // messagepopup
        $this->XHRANSWER->Results->MSGHead = '';
        $this->XHRANSWER->Results->MSGFoot = '';
        $this->XHRANSWER->Results->MSGBody = '';
        // js to eval on receive
        $this->XHRANSWER->Results->js = '';
    }
    /**
     * return a instance of object
     *
     * @return xhranswer xhranswer instance
     */
    public static function getInstance()
    {
        if (!isset(self::$INSTANCE))
        {
            $c = __CLASS__;
            self::$INSTANCE = new $c;
        }
        return self::$INSTANCE;
    }
    /**
     * return xhr string
     *
     * @return string
     */
    public function __toString()
    {
        // clean up msg
        if (!$this->XHRANSWER->Results->UpdateValue)
            unset ($this->XHRANSWER->Results->UpdateValue);
        if (!$this->XHRANSWER->Results->replaceContentById)
            unset ($this->XHRANSWER->Results->replaceContentById);
        if (!function_exists('json_encode'))
        {
            $json = new Services_JSON();
            $stream = $json->encode($this->XHRANSWER);
        }
        else
            $stream = json_encode($this->XHRANSWER);
        if ($this->XHRANSWERBYPASS)
        {
            foreach ($this->XHRANSWERBYPASS as $k=>$v)
            {
                $stream = str_replace('":#:XHRSPACER_' . $k . ':#:"', $v, $stream);
            }
        }
        return $stream;
    }
    /**
     * submit datatable datas
     *
     * @param string $id      id of datatable
     * @param string $content datacontent
     *
     * @return void
     */
    public function sendDataTableData ($id = null, $content = null)
    {
        $s_elName = 'dt' . $id . 'Data';
        $this->XHRANSWER->$s_elName = ':#:XHRSPACER_' . $s_elName . ':#:';
        $this->XHRANSWERBYPASS[$s_elName] = $content;
    }
    /**
     * submit tabview content
     *
     * @param string $content datacontent
     *
     * @return void
     */
    public function sendTabContent ($content = null)
    {
        $this->XHRANSWER->Results->tabViewContent = $content;
    }
    /**
     * register a value to update, e.g. <input id="ident" value="newval" ....
     *
     * @param string $identifier identifier (id of html object)
     * @param string $val        new value
     *
     * @return void
     */
    public function updateValue($identifier = null, $val = '')
    {
        $this->XHRANSWER->Results->UpdateValue[$identifier] = $val;
    }
    /**
     * set msg popup head
     *
     * @param string $text head text
     *
     * @return void
     */
    public function setMsgHead ($text = '')
    {
        $this->XHRANSWER->Results->MSGHead = $text;
    }
    /**
     * set msg popup time to show in sec.
     *
     * @param int $seconds time in sec. to show popup
     *
     * @return void
     */
    public function setMsgTime ($seconds = '')
    {
        $this->XHRANSWER->Results->MSGTime = $seconds;
    }
    /**
     * set msg popup footer
     *
     * @param string $text foot text
     *
     * @return void
     */
    public function setMsgFoot ($text = '')
    {
        $this->XHRANSWER->Results->MSGFoot = $text;
    }
    /**
     * set msg popup body
     *
     * @param string $text body text
     *
     * @return void
     */
    public function setMsgBody ($text = '')
    {
        $this->XHRANSWER->Results->MSGBody = $text;
    }
    /**
     * set msg popup
     *
     * @param string $body body text
     * @param string $head head text
     * @param string $foot foot text
     *
     * @return void
     */
    public function setMsg ($body = '', $head = '', $foot = '')
    {
        $this->XHRANSWER->Results->MSGBody .= $body;
        $this->XHRANSWER->Results->MSGHead = $head;
        $this->XHRANSWER->Results->MSGFoot = $foot;
    }
    /**
     * evaluate js on receive
     *
     * @param string $js js code to eval
     *
     * @return void
     */
    public function setJs ($js = '')
    {
        $this->XHRANSWER->Results->js .= $js;
    }
    /**
     * replace content
     *
     * @param string $id      id of container to replace content
     * @param string $content content to place
     *
     * @return void
     */
    public function replaceContentById ($id = null, $content = null)
    {
        $this->XHRANSWER->Results->replaceContentById[$id] = $content;
    }
    /**
     * popup with debug msg
     *
     * @param mixed $var variable to debug
     *
     * @return void
     */
    public function debugVar ($var)
    {
        // get may existing output handler
        $oldHandler = ob_list_handlers();
        $old = ob_get_clean();
        ob_start();
        debugVar($var);
        $content = ob_get_clean();
        if ($old)
        {
            // output buffering was on
            ob_start($oldHandler[0]);
            echo $old;
        }
        // add debug content to stream
        $this->setMsg($content, 'Debug Message');
        $this->setMsgTime(0);
    }
}